// pages/all/all.js
const db = wx.cloud.database()
const cont = db.collection('daily');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    dailyid:'',
    daily:[],
    daily1:[],
    openid:'',
    firstopenid:'',
    showView:false,
    showtop:'',
    dailytime:'',
    dailytext:'',
    updatedailyValue:'',
    floorstatus: false

  },

  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0,  // 滚动到页面的目标位置，单位 px
        duration: 200 // 滚动动画的时长，单位 ms
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  onPageScroll: function (e) {
    if (e.scrollTop > 50) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("id123", options)
    var id = options
    // console.log(id)
    this.setData({
      id: id
    })
    console.log("456", this.data.id)
   
  },

  timecolor:function(){
    console.log(this.data._openid)
    console.log(this.data.firstopenid)
    // if(this.data.daily._openid != this.data.daily.firstopenid){

    // }
  },

  daily:function(e){
    console.log("id321", e)
    console.log("dailyid", e.currentTarget.dataset.id)
    var dailyid = e.currentTarget.dataset.id
    // console.log(id)
    this.setData({
      dailyid: dailyid,
      showView:!this.data.showView
    })
    console.log("569", this.data.dailyid)
    // db.collection('daily').get({

    // })

    db.collection('daily').where({
      _id: this.data.dailyid,
    }).get({
      success:res=>{
        console.log(res.data[0])
        var data = res.data[0]
        this.setData({
          daily1:data,
          dailytime: data.dailytime,
          dailytext: data.dailytext,
        })
      
        console.log("daily",daily1)
      }
      
    })
    //   success: res => {
    //     var dailytime = res.data.dailytime
    //     var dailytext = res.data.dailytext
    //     console.log(dailytime)
    //     console.log(dailytext)
    //     this.setData({
    //       daily1:res.data,
    //       dailytime: dailytime,
    //       dailytext: dailytext,
    //     })
    //     console.log(this.data.dailytime)
    //     console.log(this.data.dailytext)
    //   }
    // })
    },

    updatedailyValue: function (e) {
      var updatedailyValue = this.data.updatedailyValue
      this.setData({ updatedailyValue: e.detail.value });
      console.log(updatedailyValue)
    },

    updatedaily:function(){
      if (this.data.updatedailyValue == "" || this.data.updatedailyValue == "None") {
        wx.showToast({
          title: '请输入日志',
          icon:'none',
          duration:2000
        })
        return;
        }else{
          this.setData({
            dailytext: this.data.updatedailyValue,
            showView:!this.data.showView
          })
          wx.showToast({
            title: '保存成功！',
            icon: 'success',
            duration: 1200,
            mask: true
          })
          wx.cloud.callFunction({
            name: 'updatedaily',
            data: {
              dataId: this.data.dailyid,
              dailytext: this.data.dailytext,
            },
            success: function (res) {
              console.log(res)
            },
  
          })
          this.getOpenid();
        }

    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openid)
        var openid = res.result.openid;
        that.setData({
          openid: openid
        })
        console.log(this.data.openid)
        console.log("852",this.data.openid)
        console.log("569",this.data.id.id)
        db.collection("daily").where({
          jihuaid: this.data.id.id,
          _openid: this.data.openid,
        }).get({
          success:res=>{
            console.log(res)
            this.setData({
              daily:res.data
            })
          
            console.log("daily",this.data.daily)
            var dailytop = this.data.daily
            console.log("daily1",dailytop)
            if (dailytop == ""){
              this.data.showtop = true
            }else{
              this.data.showtop = false
            }
            this.setData({
              showtop: this.data.showtop
            })
          }
        })
          }
    })

  } ,

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOpenid();
    
    // cont.doc(this.data.id.id).get({
    //   success: res => {
    //     var _openid = res.data._openid,
    //     var firstopenid = res.data.firstopenid
    //   }
    // })
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})